Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(gerrit): not auto-merging if Code-Review changed from +2 to +1 #32818

Open
wants to merge 23 commits into
base: main
Choose a base branch
from

Conversation

felipecrs
Copy link
Contributor

@felipecrs felipecrs commented Nov 29, 2024

Changes

The checkIfApproved function was not working as expected when someone already had published a Code-Review +1 vote. It was wrongly assuming that the change was approved in such case, while the change should only be approved if the change had a Code-Review +2 vote.

Note that when o=DETAILED_LABELS is set, the change response from Gerrit API no longer contains the content from o=LABELS, meaning it should only ever be used in this case (when looking for the approvals), otherwise it would break existing code.

This fix also ensures that the change will be auto-approved again during the auto-merge process, in case the Code-Review +2 was changed to +1 by some CI check using the same account as Renovate, like SonarQube (which posts Code-Review +1).

Finally, this fix will improve the approval logic for when the change is initially created or updated. It will avoid two API calls in each case.

Context

I self-host Renovate and I noticed that auto-merge was not working as expected. I realized it was caused by this issue, because for a given project we have here, the same user/robot also votes the change with a Code-Review +1 vote when SonarQube analysis is successful (which is a very known pattern).

Documentation (please check one with an [x])

  • I have updated the documentation, or
  • No documentation update is required

How I've tested my work (please select one)

I have verified these changes via:

  • Code inspection only, or
  • Newly added/modified unit tests, or
  • No unit tests but ran on a real repository, or
  • Both unit tests + ran on a real repository

@felipecrs felipecrs force-pushed the fix-auto-merge-if-plus-one branch from 5b42180 to 4028843 Compare November 29, 2024 23:14
@felipecrs felipecrs marked this pull request as draft November 29, 2024 23:33
@felipecrs felipecrs force-pushed the fix-auto-merge-if-plus-one branch from 4028843 to 06ecde1 Compare November 29, 2024 23:53
@felipecrs felipecrs marked this pull request as ready for review November 29, 2024 23:53
@felipecrs felipecrs force-pushed the fix-auto-merge-if-plus-one branch from 06ecde1 to 46f8eed Compare November 29, 2024 23:58
@felipecrs felipecrs force-pushed the fix-auto-merge-if-plus-one branch from 82fe8e1 to 107a298 Compare November 30, 2024 01:52
@felipecrs felipecrs marked this pull request as draft November 30, 2024 06:18
@felipecrs felipecrs force-pushed the fix-auto-merge-if-plus-one branch from dcc53d7 to 6ac7b9e Compare November 30, 2024 07:12
@felipecrs felipecrs force-pushed the fix-auto-merge-if-plus-one branch from 4817e82 to 6ffa5b6 Compare November 30, 2024 07:18
@felipecrs felipecrs marked this pull request as ready for review November 30, 2024 07:24
rarkins
rarkins previously approved these changes Nov 30, 2024
Copy link
Member

@viceice viceice left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

conflicted

@felipecrs
Copy link
Contributor Author

I just realized this function is also implemented by the GitLab provider, and there it doesn't check for whether it's approved already or not, meaning it will probably issue an error, which is not desired.

I'll think about it.

@felipecrs felipecrs marked this pull request as draft December 1, 2024 15:29
@felipecrs felipecrs force-pushed the fix-auto-merge-if-plus-one branch from 56bd600 to 761106b Compare December 1, 2024 16:38
@felipecrs felipecrs force-pushed the fix-auto-merge-if-plus-one branch from 761106b to 45763e9 Compare December 1, 2024 16:40
@felipecrs
Copy link
Contributor Author

@viceice, @rarkins, can you please help me understand this failed check?

❌ Error: Snapshots have been added in this PR. Use toMatch instead.

@felipecrs felipecrs marked this pull request as ready for review December 1, 2024 16:43
@felipecrs
Copy link
Contributor Author

felipecrs commented Dec 1, 2024

I've refactored the other two platforms (Azure and GitLab) that also use autoApprove to account for these improvements as well:

  1. As last resort, the PR will be attempted to be auto-approved before auto-merge, in case the approval got dismissed after it was initially published for whatever reason.
  2. It will no longer attempt to auto-approve if the PR is already approved

Note I do not use Azure or GitLab. I coded these functions based on their documentation. But I believe there is no big risk involved, given it will still attempt to auto-approve in case the is approved condition doesn't match.

@felipecrs felipecrs force-pushed the fix-auto-merge-if-plus-one branch from 45763e9 to 5a0955c Compare December 1, 2024 16:49
@felipecrs felipecrs marked this pull request as draft December 1, 2024 16:51
@felipecrs felipecrs force-pushed the fix-auto-merge-if-plus-one branch from 5a0955c to ab9907a Compare December 1, 2024 17:06
lib/modules/platform/gitlab/index.ts Outdated Show resolved Hide resolved
lib/workers/repository/update/pr/automerge.ts Outdated Show resolved Hide resolved
@felipecrs
Copy link
Contributor Author

@rarkins, it's presumptuous from my part that this would be of any help to the existing platforms. I will revert the changes to Azure and GitLab, and focus on Gerrit, which is what I use and know this would make sense for.

@felipecrs felipecrs marked this pull request as draft December 2, 2024 17:02
@felipecrs felipecrs changed the title feat(autoApprove): auto-approve PR before auto-merge attempt if needed fix(gerrit): not auto-merging if Code-Review vote was downgraded Dec 2, 2024
@felipecrs felipecrs changed the title fix(gerrit): not auto-merging if Code-Review vote was downgraded fix(gerrit): not auto-merging if Code-Review changed from +2 to +1 Dec 2, 2024
@felipecrs felipecrs requested a review from rarkins December 2, 2024 18:32
@felipecrs felipecrs marked this pull request as ready for review December 2, 2024 18:32
lib/modules/platform/gerrit/client.ts Outdated Show resolved Hide resolved
lib/modules/platform/gerrit/client.ts Outdated Show resolved Hide resolved
lib/modules/platform/gerrit/index.spec.ts Show resolved Hide resolved
lib/workers/repository/update/pr/automerge.spec.ts Outdated Show resolved Hide resolved
lib/modules/platform/gerrit/scm.ts Show resolved Hide resolved
@felipecrs felipecrs requested a review from viceice December 29, 2024 15:53
@felipecrs
Copy link
Contributor Author

felipecrs commented Dec 29, 2024

@viceice, sorry for taking so long. I thought it would be harder to address your comments, but they were in fact straightforward and helpful. Thank you very much.

EDIT: working on the failed coverage.
EDIT 2: done.

@felipecrs
Copy link
Contributor Author

@viceice, this is a kind ping. :)

Copy link
Member

@viceice viceice left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

conflicted

@felipecrs felipecrs requested a review from viceice January 17, 2025 18:26
Copy link
Member

@viceice viceice left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

otherwise LGTM

@@ -69,6 +69,10 @@ export async function checkAutoMerge(
prAutomergeBlockReason: 'PlatformNotReady',
};
}
if (config.autoApprove && platform.approvePrForAutomerge) {
logger.debug('Auto-approving PR for automerge');
await platform.approvePrForAutomerge(pr.number);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it this a noop if it's already approved? see other open conversation

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But the PR could have been un-approved after initial approve. See the PR body's Context:

I self-host Renovate and I noticed that auto-merge was not working as expected. I realized it was caused by this issue, because for a given project we have here, the same user/robot also votes the change with a Code-Review +1 vote when SonarQube analysis is successful (which is a very known pattern).

SonarQube analysis in such case happens during CI, after Renovate has initially approved the PR upon creation or update.

This extra-check will account for cases where the PR had its vote downgraded from +2 to +1.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe this is a special case that can only happen with Gerrit. On GitHub and Gitlab, there's no such concept of approval level. It's either approved or not, by a given user.

However, in Gerrit, "approvals" can either be +2 (highest, needed to merge), or +1 (not enough to merge). And a user, can have its vote decreased from +2 to +1.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants